import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class RouterDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.blue,
      child: Column(
        children: <Widget>[
          MaterialButton(
            child: Text("到页面2(方式1)"),
            onPressed: () {
              Navigator.push(
                context,
                new MaterialPageRoute(builder: (context) => new RouterDemo2()),
              );
            },
          ),
          MaterialButton(
            child: Text("到页面2(方式2)"),
            onPressed: () {
              Navigator.pushNamed(context, "router_demo2");
            },
          ),
          MaterialButton(
              child: Text("到页面2(带返回值)"),
              onPressed: () {
                _navigateAndDisplaySelection(context);
              }),
          MaterialButton(
              child: Text("动画"),
              onPressed: () {
                Navigator.push(context, _createRoute());
              })
        ],
      ),
    );
  }

  Route _createRoute() {
    return PageRouteBuilder(
      pageBuilder: (context, animation, secondaryAnimation) => RouterDemo2(),
      transitionsBuilder: (context, animation, secondaryAnimation, child) {
        var begin = Offset(1.0, 1.0);
        var end = Offset.zero;
        var curve = Curves.easeInBack;

        var tween =
            Tween(begin: begin, end: end).chain(CurveTween(curve: curve));

        return SlideTransition(
          position: animation.drive(tween),
          child: child,
        );
      },
    );
  }

  _navigateAndDisplaySelection(BuildContext context) async {
    // Navigator.push returns a Future that completes after calling
    // Navigator.pop on the Selection Screen.
    final result = await Navigator.push(
      context,
      MaterialPageRoute(builder: (context) => RouterDemo2()),
    );

    debugPrint("result is $result");
  }
}

class RouterDemo2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text("页面2"),
          MaterialButton(
            child: Text("返回"),
            onPressed: () {
              Navigator.pop(context);
            },
          ),
          MaterialButton(
            child: Text("返回带值"),
            onPressed: () {
              Navigator.pop(context, "hello");
            },
          ),
        ],
      ),
    );
  }
}

class RouterDemo3 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text("页面3"),
          MaterialButton(
            child: Text("启动页面1"),
            onPressed: () {
              Navigator.popUntil(context, ModalRoute.withName("router_demo"));
            },
          ),
        ],
      ),
    );
  }
}
